# Maintainer:  Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=wxPython
_wx_basever=3.1
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.2a1.dev5434+7d45ee6a
pkgrel=2
pkgdesc="A wxWidgets GUI toolkit for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom:wxWindows")
url="https://www.wxpython.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-python-numpy" )
         "${MINGW_PACKAGE_PREFIX}-python-pillow"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}")
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python-requests"
             "${MINGW_PACKAGE_PREFIX}-sip4"
             "${MINGW_PACKAGE_PREFIX}-waf"
             "${MINGW_PACKAGE_PREFIX}-cc"
             # for waf
             "python" "python-setuptools")
options=('strip' 'staticlibs' 'buildflags')
source=(#"https://files.pythonhosted.org/packages/b0/4d/80d65c37ee60a479d338d27a2895fb15bbba27a3e6bb5b6d72bb28246e99/${_realname}-${pkgver}.tar.gz"
        "https://wxpython.org/Phoenix/snapshot-builds/${_realname}-${pkgver}.tar.gz"
        '201-Remove-obsolete-entries-from-Doxyfile.patch'
        '301-Revert-some-of-python-sip-changes.patch')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('9ab0b6a42d6bb6eacd45ba70f0e9940af56e1711596374c72b4a9110761e36db'
            '97b5c9dd353f2c570b7a940585868c1127d10a96a3442cd96706359410494aa8'
            '84f836f22f4bbe3f0644040188997fe82ad8db2d4ece6ae081f99d405aff7aa5')

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz due to symlink(s) without pre-existing target(s)"
  cd "${srcdir}"
  [[ -d ${_realname}-${pkgver} ]] && rm -rf ${_realname}-${pkgver}
  tar zxf "${srcdir}/${_realname}-${pkgver}.tar.gz" || true

  cd "${srcdir}/${_realname}-${pkgver}"
  rm -f etg/{_,}webkit.py sip/gen/{_,}webkit.sip

  # wxPython possible bug fixes 1xx

  # Revert some wxPython changes 3xx
  patch -Np1 -i ${srcdir}/301-Revert-some-of-python-sip-changes.patch

  # backport wxPython git changes 4xx

  # wxWidgets changes 2xx
  cd "${srcdir}/${_realname}-${pkgver}/ext/wxWidgets"
  # Reduce warnings
  patch -Np1 -i ${srcdir}/201-Remove-obsolete-entries-from-Doxyfile.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  export PYTHONDONTWRITEBYTECODE=1
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  WAF="{$MINGW_PREFIX}/bin/waf" \
  SIP="${MINGW_PREFIX}/bin/sip" \
  DOXYGEN="${MINGW_PREFIX}/bin/doxygen" \
  WX_CONFIG="${MINGW_PREFIX}/bin/wx-config-${_wx_basever}" \
    "${MINGW_PREFIX}/bin/python" build.py \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --release --use_syswx --no_msedge --nodoc --cairo -vv \
      --no_allmo --no_magic --regenerate_sysconfig \
        dox touch etg sip

  local _jobs=${MAKEFLAGS:--j1}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  CC_NAME=${CC} CXX_NAME=${CXX} \
  LDFLAGS="${LDFLAGS} $(python-config --ldflags)" \
  PYTHON_CONFIG="${MINGW_PREFIX}/bin/python-config" \
    "${MINGW_PREFIX}/bin/waf" \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --check-cxx-compiler=${CXX} --check-c-compiler=${CC} --color=yes --jobs=${_jobs#-j} \
      --wx_config=wx-config-${_wx_basever} \
      --no_magic --nopyc --nopyo --nopycache \
        configure build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}/bin/python" setup.py install \
      --prefix="${MINGW_PREFIX}" --root="${pkgdir}" --skip-build

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -vDm 644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/LICENSE.txt"
}
